<?php
require 'vendor/autoload.php';

use think\Image;
//常量，标识左上角水印
const WATER_NORTHWEST = 1;
//常量，标识上居中水印
const WATER_NORTH = 2;
//常量，标识右上角水印
const WATER_NORTHEAST = 3;
//常量，标识左居中水印
const WATER_WEST = 4;
//常量，标识居中水印
const WATER_CENTER = 5;
//常量，标识右居中水印
const WATER_EAST = 6;
//常量，标识左下角水印
const WATER_SOUTHWEST = 7;
//常量，标识下居中水印
const WATER_SOUTH = 8;
//常量，标识右下角水印
const WATER_SOUTHEAST = 9;

class Test{
    public $pic_path="";
    function upload()
    {
        if ((($_FILES["file"]["type"] == "image/gif")
                || ($_FILES["file"]["type"] == "image/jpeg")
                || ($_FILES["file"]["type"] == "image/pjpeg"))
            && ($_FILES["file"]["size"] < 2000000)) {
            if ($_FILES["file"]["error"] > 0) {
                echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
            } else {
                echo "Upload: " . $_FILES["file"]["name"] . "<br />";
                echo "Type: " . $_FILES["file"]["type"] . "<br />";
                echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
                echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

                if (file_exists("upload/" . $_FILES["file"]["name"])) {
                    move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . "test.jpg");
                    $this->pic_path= "upload/" . "test.jpg" ;
                    echo "Stored in: " . "upload/" .  "test.jpg";
                } else {
                    move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . "test.jpg");
                    $this->pic_path= "upload/" . "test.jpg" ;
                    echo "Stored in: " . "upload/" .  "test.jpg";
                }
            }
        } else {
            echo "Invalid file";
        }
    }
    public function modify($pic_path){
        $image = Image::open($pic_path);
        $image->thumb(275, 275)->save('./thumb.png');
        $background = Image::open('./background.png');
        $background->water("./thumb.png", Image::WATER_CENTER)->save('water_image.png');
    }
}

$test=new Test();
$test->upload();
$pic=$test->pic_path;
$test->modify($pic);
header("location:image.php")
?>